﻿Imports System.Drawing
Imports System.Math
Imports System.IO


Public Class frmExamI
    Private mCurrentExamI As New clsExamI
    Private mSearchExamI As New clsExamI
    Private mCurrentOpt As New LevelDatabase.CurrentOption

    Private Sub frmExamI_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If mCurrentExamI.I_of_Level.Length > 0 Then
            If MsgBox("I角检测数据需更新吗？" & vbCrLf & "Yes(更新)/No(放弃)", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                SaveData()
            End If
        End If
    End Sub

    ''' <summary>
    ''' 随输入法进行控件高度调整
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub FormExamI_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadPrepare()
    End Sub
    Private Sub LoadPrepare()
        'Dim mDb As New DbLevel
        Try
            mCurrentOpt = MyShared.myDb.CurrentRead
            If mCurrentOpt.CurrentProject.Length < 5 Then
                MsgBox("请设置项目名称！")
                Me.Close()
            Else
                With mCurrentExamI.Opts
                    .sDate = Now.ToString("yy-MM-dd")
                    .Surveyor = mCurrentOpt.Surveyor
                    .Recorder = mCurrentOpt.Recordor
                    .Examor = mCurrentOpt.Examor
                    .InstrNo = mCurrentOpt.InstrNo
                    .Staff1 = mCurrentOpt.Staff1
                    .Staff2 = mCurrentOpt.Staff2
                End With
            End If
            setDsp()
        Catch ex As Exception

        End Try

    End Sub


    Private Sub InputPanel1_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanel1.EnabledChanged
        setDsp()
    End Sub
    Private Sub setDsp()
        If InputPanel1.Enabled Then
            TabControl1.Height = Me.Height - InputPanel1.Bounds.Height
        Else
            TabControl1.Height = Me.Height
        End If
    End Sub


    ''' <summary>
    ''' 页面变换
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Select Case TabControl1.SelectedIndex
            Case 0
                '观测页
                InputPanel1.Enabled = True
                rbnA1.Checked = True
                UpdateListByReaddata()

            Case 1
                '手簿页
                InputPanel1.Enabled = False

                'Dim dbI As New DbLevel
                MyShared.myDb.IExamReadLast(mSearchExamI)
                Try
                    nNumber.Maximum = mSearchExamI.Opts.iNumber + 1
                    nNumber.Value = mSearchExamI.Opts.iNumber
                Catch ex As Exception

                End Try

                'txtView.Text = mSearchExamI.ToString
            Case Else
        End Select
    End Sub
    Private Sub nNumber_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nNumber.ValueChanged
        'Dim dbI As New DbLevel
        If nNumber.Value < nNumber.Maximum Then
            MyShared.myDb.IExamRead(nNumber.Value, mSearchExamI)
            txtView.Text = mSearchExamI.ToString
            Label2.Text = "i=" & mSearchExamI.I_of_Level
        Else
            txtView.Text = mCurrentExamI.ToString
            Label2.Text = "i=" & mCurrentExamI.I_of_Level
        End If
    End Sub

#Region "读数"

    ''' <summary>
    ''' 增加读数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAdd.Click
        AddOneRead()
    End Sub
    Private Sub AddOneRead()
        txtRead.Text = txtRead.Text.Replace(" ", "")
        If txtRead.Text.Length = 0 Then Return

        If rbnA1.Checked Then
            mCurrentExamI.AddRead(1, "A", txtRead.Text)
        ElseIf rbnA2.Checked Then
            mCurrentExamI.AddRead(1, "B", txtRead.Text)
        ElseIf rbnB1.Checked Then
            mCurrentExamI.AddRead(2, "A", txtRead.Text)
        Else
            mCurrentExamI.AddRead(2, "B", txtRead.Text)
        End If

        Dim txt As String = txtRead.Text
        If txt.Length > 3 Then
            txt = Mid(txt, 1, txt.Length - 3)
            txtRead.Text = txt
        End If
        txtRead.Focus()
        txtRead.SelectionStart = txt.Length

        UpdateListByReaddata()
    End Sub

    ''' <summary>
    ''' 删除一个读数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonErase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bErase.Click
        Dim iPos As Integer = LstRead.SelectedIndex

        If rbnA1.Checked Then
            mCurrentExamI.EraseRead(1, "A", iPos)
        ElseIf rbnA2.Checked Then
            mCurrentExamI.EraseRead(1, "B", iPos)
        ElseIf rbnB1.Checked Then
            mCurrentExamI.EraseRead(2, "A", iPos)
        Else
            mCurrentExamI.EraseRead(2, "B", iPos)
        End If
        UpdateListByReaddata()
    End Sub


    ''' <summary>
    ''' 测尺变换
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles rbnA1.CheckedChanged, rbnA2.CheckedChanged, rbnB1.CheckedChanged, rbnB2.CheckedChanged
        UpdateListByReaddata()
        txtRead.Text = ""
        txtRead.Focus()
    End Sub

    ''' <summary>
    ''' 读数只接受数字
    ''' 回车时确认
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtRead_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRead.KeyPress
        If e.KeyChar = Chr(13) Then
            AddOneRead()
        Else
            NumericTextboxExam(txtRead.Text, e)
        End If

    End Sub
    Private Sub NumericTextboxExam(ByVal txtExist As String, ByRef e As System.Windows.Forms.KeyPressEventArgs)
        Select Case Asc(e.KeyChar)
            Case 32, 8, 48 To 57 '空格,退格,0-9不处理
            Case 46 '小数点，只能一个
                If InStr(txtExist, ".") <> 0 Then
                    e.Handled = True
                End If
            Case Else '其它的终止事件
                e.Handled = True
        End Select
    End Sub

    ''' <summary>
    ''' 根据读数刷新ReadList
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UpdateListByReaddata()
        Dim ExamData As clsExamI = IIf(nNumber.Value < mCurrentExamI.Opts.iNumber, mSearchExamI, mCurrentExamI)
        With ExamData
            Dim vP As IExamRead
            Dim I_Ang As String = ExamData.I_of_Level
            Dim I_dbl As Double = 9999
            If IsNumeric(I_Ang) Then
                I_dbl = CDbl(I_Ang)
            End If
            Dim ModRead As String = String.Empty

            If rbnA1.Checked Then
                vP = .Stn1.ReadA
                LstRead.BackColor = rbnA1.BackColor
                rbnA1.Text = "A1" & "_" & vP.ReadCount
                bAdd.BackColor = rbnA1.BackColor
                bErase.BackColor = rbnA1.BackColor
                If I_dbl < 9000 Then ModRead = (vP.AverageRead - 10 * I_dbl).ToString("0")


            ElseIf rbnA2.Checked Then
                vP = .Stn1.ReadB
                LstRead.BackColor = rbnA2.BackColor
                rbnA2.Text = "A2" & "_" & vP.ReadCount
                bAdd.BackColor = rbnA2.BackColor
                bErase.BackColor = rbnA2.BackColor
                If I_dbl < 9000 Then ModRead = (vP.AverageRead - 20 * I_dbl).ToString("0")

            ElseIf rbnB1.Checked Then
                vP = .Stn2.ReadA
                LstRead.BackColor = rbnB1.BackColor
                rbnB1.Text = "B1" & "_" & vP.ReadCount
                bAdd.BackColor = rbnB1.BackColor
                bErase.BackColor = rbnB1.BackColor
                If I_dbl < 9000 Then ModRead = (vP.AverageRead - 20 * I_dbl).ToString("0")

            Else
                vP = .Stn2.ReadB
                LstRead.BackColor = rbnB2.BackColor
                rbnB2.Text = "B2" & "_" & vP.ReadCount
                bAdd.BackColor = rbnB2.BackColor
                bErase.BackColor = rbnB2.BackColor
                If I_dbl < 9000 Then ModRead = (vP.AverageRead - 10 * I_dbl).ToString("0")

            End If

            LstRead.Items.Clear()
            For Each Read As Long In vP.Reads
                LstRead.Items.Add(Read.ToString)
            Next

            If .I_of_Level.Length > 0 Then
                lblSvI.Visible = True
                lblSvI.Text = "I=" & .I_of_Level & "″" & vbCrLf & "纠至:" & ModRead
            Else
                lblSvI.Visible = False
                lblSvI.Text = ""
            End If

        End With
    End Sub

#End Region

    ''' <summary>
    ''' 保存数据
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
        SaveData()
    End Sub
    Private Sub SaveData()
        'Dim mDb As New DbLevel
        Dim mCurrentOpt As New LevelDatabase.CurrentOption
        mCurrentOpt = MyShared.myDb.CurrentRead()

        Dim TxtDate As String = Now.ToString("yy-MM-dd")

        With mCurrentExamI.Opts
            .sDate = TxtDate
            .Surveyor = mCurrentOpt.Surveyor
            .Recorder = mCurrentOpt.Recordor
            .Examor = mCurrentOpt.Examor
            .InstrNo = mCurrentOpt.InstrNo
            .Staff1 = mCurrentOpt.Staff1
            .Staff2 = mCurrentOpt.Staff2
        End With

        'Dim mDB1 As New DbLevel
        MyShared.myDb.IExamRecordRow(mCurrentExamI)


        '保存文本文件
        If mCurrentExamI.I_of_Level.Length > 0 Then
            Dim sTxt As String = mCurrentExamI.ToString
            Try
                Dim Card As String = String.Empty
                If StorageCard.GetFirstCardName(Card) Then
                    Dim I As Integer = CLng(mCurrentExamI.I_of_Level)
                    Dim mPath As String = "\" & Card & "\工程数据\I角检测\" & mCurrentOpt.InstrNo
                    Dim FullFileName As String = mPath & "\" & _
                    mCurrentOpt.InstrNo & "_" & TxtDate & "_" & I.ToString & ".txt"

                    System.IO.Directory.CreateDirectory(mPath)
                    Dim FN As System.IO.StreamWriter = New IO.StreamWriter(FullFileName, False)
                    FN.WriteLine(mCurrentExamI.ToString)
                    FN.Close()
                End If
            Catch ex As Exception
                MsgBox("写文本文件失败！")
            End Try
        End If
    End Sub

    ''' <summary>
    ''' 观测
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
        mCurrentExamI = New clsExamI
        nNumber.Value = nNumber.Maximum
        TabControl1.SelectedIndex = 0
    End Sub




End Class

